package com.sniff.atp.modules.test.process;

import com.ckbs.global.common.utils.DingTalkNoticeUtils;
import com.sniff.atp.modules.test.model.DemandReport;
import com.sniff.atp.modules.test.model.PublishPlan;
import com.sniff.atp.modules.test.service.DemandReportService;
import com.sniff.atp.modules.test.service.PublishPlanService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ProjectName: sniff-atp
 * @Package: com.sniff.atp.modules.test.process
 * @ClassName: BusinessPublishPlan
 * @Description: 通知产研中心今日发布发布内容确认
 * @Author: zss
 * @CreateDate: 2023/11/2 19:36
 * @UpdateDate: 2023/11/2 19:36
 * @Version: 1.0
 */

@Component
@Configuration
@EnableScheduling
public class DevelopPublishPlan {

    @Value("${sniff.autoInfoFlag}")
    private String autoInfoFlag;

    @Autowired
    private PublishPlanService publishPlanService;

    @Autowired
    private DemandReportService demandReportService;

//    @Scheduled(cron = "0/5 * * * * ?")
    @Scheduled(cron = "0 30 22 * * ?")
    private void configureTasks(){
        System.out.println("111111111111111");

        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -4);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 获取当天的0点时间的Date对象
        Date midnightDate = calendar.getTime();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        PublishPlan publishPlan = new PublishPlan();
        publishPlan.setInfoTime(midnightDate);


        //通知技术
        List<PublishPlan> publishPlanDeveList = publishPlanService.qrydevelopPublishPlan(publishPlan);

        //获取需求id
        List<Long> demandIdList = publishPlanDeveList.stream().map(PublishPlan::getDemandId).collect(Collectors.toList());
        //获取需求list
        List<DemandReport> demandReportList = demandReportService.qryDemandInfoByIdList(demandIdList);

        //发布计划的list中增加一个PD的字段
        publishPlanDeveList.forEach(entity1 ->
                demandReportList.stream()
                        .filter(entity2 -> entity2.getDemandId().equals(entity1.getDemandId()))
                        .findFirst()
                        .ifPresent(entity2 -> {
                            entity1.setDemandPd(entity2.getDemandPd());
                        })
        );

        if(!publishPlanDeveList.isEmpty()){
            //通知内容
            String contentDev = "";
            contentDev = contentDev.concat("------发布通告-----\n发布日期: ").concat(currentDate.format(formatter)).concat("\n");
            contentDev = contentDev.concat("发布时间: 22点 \n").concat("发布内容: \n \n");

            String addContentDev = "";
            //有值，通知客户
            for(int i=0;i<publishPlanDeveList.size();i++){
                addContentDev = addContentDev.concat("【").concat(publishPlanDeveList.get(i).getPrefixDemandBug()).concat("】");
                addContentDev = addContentDev.concat(publishPlanDeveList.get(i).getDemandPrdInfo());
                //增加PD
                String demandPD = publishPlanDeveList.get(i).getDemandPd();
                if(StringUtil.isNotBlank(demandPD) && !"".equals(demandPD)){
//                    hasDevelopFlag = true;
                    addContentDev = addContentDev.concat(" （产品：");
                    addContentDev = addContentDev.concat(demandPD).concat(")").concat("\n");
                }else{
                    addContentDev = addContentDev.concat("\n");
                }
            }
            //两个通知内容合并
            contentDev = contentDev.concat(addContentDev);

            // 发布钉钉通知产研中心
            String robotToken = "ddd895bb46c4a220954486f6ccc4b65f55179d347e27ade280671a84ccb8bd60";
            String robotKey = "SEC63afa1d0a9b32aa1b7d7fc8c2b8e0f10100fce2fff376090f382803fa0ef55e2";

//            String robotToken = "c6c2c7c6c98c2fb5d234319afa9e0dad659152a8518ac739191811ce1bf8a67b";
//            String robotKey = "SECdf56e788259528d23a4b225863526a0bc426c178251313f1e4d5bdc5e9b696d6";

            //产研中心
            List<String> mobileList = new ArrayList<>();

            if("true".equals(autoInfoFlag)){
                DingTalkNoticeUtils.sendDingMsgAll(robotKey,robotToken,mobileList,contentDev);
            }

        }
    }
}
